package com.dwf.dominion;

import java.util.Vector;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CardListArrayAdapter extends ArrayAdapter<Card>{

	private Vector<Card> cards;
	
	public CardListArrayAdapter(Context context, int textViewResourceId,
			Vector<Card> cards) {
		super(context, textViewResourceId, cards);
		this.cards = cards;
	}

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View currentView = convertView;
        if (currentView == null) {
            LayoutInflater cardListLayout = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            currentView = cardListLayout.inflate(R.layout.card_list_layout, null);
        }
        Card thisCard = cards.get(position);
        if (thisCard != null) {
        		LinearLayout layout = (LinearLayout) currentView.findViewById(R.id.card_list_row);
                TextView name = (TextView) currentView.findViewById(R.id.card_name);
                TextView cardset = (TextView) currentView.findViewById(R.id.card_set);
                ImageView cost = (ImageView) currentView.findViewById(R.id.coin);
                ImageView type = (ImageView) currentView.findViewById(R.id.type);
                
                name.setText(thisCard.name);
                cardset.setText(thisCard.set.toString());
                
                switch(thisCard.cost) {
                case 2:
                	cost.setImageResource(R.drawable.coin_2);
                	break;
                case 3:
                	cost.setImageResource(R.drawable.coin_3);
                	break;
                case 4:
                	cost.setImageResource(R.drawable.coin_4);
                	break;
                case 5:
                	cost.setImageResource(R.drawable.coin_5);
                	break;
                case 6:
                	cost.setImageResource(R.drawable.coin_6);
                	break;
                case 7:
                	cost.setImageResource(R.drawable.coin_7);
                	break;
                case 8:
                	//image.setImageResource(R.drawable.coin_8);
                	break;
                case 80:
                	cost.setImageResource(R.drawable.coin_8_star);
                	break;
        		default:
        			break;
                }
                
    			switch(thisCard.state) {
	    			case NORMAL:
	    				layout.setBackgroundDrawable(null);
	    				break;
	    			case TEMP_LOCK:
	    				layout.setBackgroundResource(R.color.card_temp_lock);
	    				break;
	    			case TEMP_BAN:
	    				layout.setBackgroundResource(R.color.card_temp_ban);
	    				break;
	    			default:
	    				break;
    			}
                
                if (thisCard.type.length == 1)
                {
                	switch (thisCard.type[0])
                	{
                		case ACTION:
    	                	type.setImageResource(R.drawable.action);
    	                	break;
                		case VICTORY:
    	                	type.setImageResource(R.drawable.victory);
    	                	break;
                		case TREASURE:
    	                	type.setImageResource(R.drawable.treasure);
    	                	break;
                	}
                }
                else
                {
                	if (thisCard.isType(Card.Type.ACTION_ATTACK))
                	{
	                	type.setImageResource(R.drawable.action_attack);                		
                	}
                	else if (thisCard.isType(Card.Type.ACTION_REACTION))
                	{
	                	type.setImageResource(R.drawable.action_reaction);                	
                	}
                	else if (thisCard.isType(Card.Type.ACTION_VICTORY))
                	{
	                	type.setImageResource(R.drawable.action_victory);                	
                	}
                	else if (thisCard.isType(Card.Type.TREASURE_VICTORY))
                	{
	                	type.setImageResource(R.drawable.victory_treasure);                	
                	}
                }
        }
        return currentView;
    }
}
